package dev.me2hat;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;

@WebServlet(name = "MyServlet", value = "/MyServlet")
public class MyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置响应内容类型
        response.setContentType("text/html; charset=utf-8");

        // 实际的逻辑是在这里
        PrintWriter out = response.getWriter();
        out.println("<h1>Servlet项目</h1>");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置响应内容类型
        response.setContentType("text/html; charset=utf-8");

        // 实际的逻辑是在这里
        PrintWriter out = response.getWriter();
        // 处理中文
        String name =new String(request.getParameter("name").getBytes("ISO8859-1"), StandardCharsets.UTF_8);
        out.println("<h1>Your name: "+name+"</h1>");
    }
}
